import Vue from 'vue';
import lodash from 'lodash';
// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
    bind(el, binding, vnode, oldVnode) {
        const dialogHeaderEl = el.querySelector('.el-dialog__header');
        const dragDom = el;
        dialogHeaderEl.style.cursor = 'move';

        dialogHeaderEl.onmousedown = (e) => {
            // 鼠标按下，计算当前元素距离可视区的距离
            const disX = e.clientX - dragDom.offsetLeft;
            const disY = e.clientY - dragDom.offsetTop;


            document.onmousemove = function (e) {
                // 通过事件委托，计算移动的距离
                let l = e.clientX - disX;
                let t = e.clientY - disY;

                // // 移动当前元素
                // dragDom.style.left = `${l + styL}px`;
                // dragDom.style.top = `${t + styT}px`;

                if (l < 0) {
                    l = 0
                } else if (l > document.documentElement.clientWidth - dragDom.offsetWidth) {
                    l = document.documentElement.clientWidth - dragDom.offsetWidth;
                }
                if (t < 0) {
                    t = 0
                } else if (t > document.documentElement.clientHeight - dragDom.offsetHeight) {
                    t = document.documentElement.clientHeight - dragDom.offsetHeight;
                }
                //移动当前元素
                dragDom.style.left = l + 'px';
                dragDom.style.top = t + 'px';
                //将此时的位置传出去
                // binding.value({
                //     x: e.pageX,
                //     y: e.pageY
                // })
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
    }
})

Vue.directive('dialogDragResize', {
    bind(el, binding, vnode, oldVnode) {
        // 弹框可拉伸最小宽高
        const minHeight = 300;
        let direction = null;

        const dialogHeaderEl = el.querySelector('.el-dialog__header');
        // 弹窗
        const dragDom = el;
        // 清除选择头部文字效果
        // dialogHeaderEl.onselectstart = new Function("return false");
        // 头部加上可拖动cursor
        dialogHeaderEl.style.cursor = 'move'
        // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
        dialogHeaderEl.onmousedown = e => {
            console.log(1);
            // 鼠标按下，计算当前元素距离可视区的距离
            const disX = e.clientX - dragDom.offsetLeft;
            const disY = e.clientY - dragDom.offsetTop;

            document.onmousemove = function (e) {
                // 通过事件委托，计算移动的距离
                let l = e.clientX - disX;
                let t = e.clientY - disY;

                if (l < 0) {
                    l = 0
                } else if (l > document.documentElement.clientWidth - dragDom.offsetWidth) {
                    l = document.documentElement.clientWidth - dragDom.offsetWidth;
                }
                if (t < 0) {
                    t = 0
                } else if (t > document.documentElement.clientHeight - dragDom.offsetHeight) {
                    t = document.documentElement.clientHeight - dragDom.offsetHeight;
                }
                //移动当前元素
                dragDom.style.left = l + 'px';
                dragDom.style.top = t + 'px';
            };

            dialogHeaderEl.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }

        dragDom.onmousemove = lodash.throttle(function(e) {
            console.log(2);
            const dialogBodyEl = el.querySelector('.el-dialog__body');
            if (
                e.clientX > dragDom.offsetLeft + dragDom.clientWidth - 20 ||
                dragDom.offsetLeft + 20 > e.clientX
            ) {
                dragDom.style.cursor = 'ew-resize';
                dialogBodyEl.style.overflow = 'hidden';
                direction = 'ew';
            } else if (
                el.scrollTop + e.clientY >
                dragDom.offsetTop + dragDom.clientHeight - 20
            ) {
                dragDom.style.cursor = 'ns-resize';
                direction = 'ns';
            } else {
                dialogBodyEl.style.overflow = 'auto';
                dragDom.style.cursor = 'default';
                direction = null;
                dragDom.onmousedown = null;
            }

            dragDom.onmousedown = e => {
                e.stopPropagation();
                const dir = direction;
                const W = document.body.clientWidth;
                const clientX = e.clientX;
                const clientY = e.clientY;
                const elW = dragDom.clientWidth;
                const elH = dragDom.clientHeight;
                const EloffsetLeft = dragDom.offsetLeft;
                const EloffsetTop = dragDom.offsetTop;
                dragDom.style.userSelect = 'none';
                const ELscrollTop = el.scrollTop;
                // 判断点击的位置是不是为头部
                if (
                    clientX > EloffsetLeft &&
                    clientX < EloffsetLeft + elW &&
                    clientY > EloffsetTop &&
                    clientY < EloffsetTop + 100
                ) {
                // 如果是头部在此就不做任何动作，以上有绑定dialogHeaderEl.onmousedown = moveDown;
                } else {
                    document.onmousemove = function(e) {
                        // 移动时禁用默认事件
                        e.preventDefault();
                        e.stopPropagation();
                        // 左侧鼠标拖拽位置
                        if (clientX > EloffsetLeft + 100 && dir === 'ew') {
                            // 往左拖拽
                            if (clientX < e.clientX && e.clientX < W - 10) {
                                dragDom.style.width = elW + ( e.clientX - clientX) + 'px';
                            }
                            // 往右拖拽
                            if (clientX > e.clientX && e.clientX > EloffsetLeft + 100 ) {
                                dragDom.style.width = elW - ( clientX - e.clientX ) + 'px';
                            }
                        }
                        // 右侧鼠标拖拽位置
                        if (
                            clientX < EloffsetLeft + elW - 20 &&
                            clientX < EloffsetLeft + elW &&
                            dir === 'ew'
                        ) {
                            // 往左拖拽
                            if (clientX > e.clientX) {
                                if (e.clientX > 0) {
                                    dragDom.style.width = elW + (clientX - e.clientX) + 'px';
                                    dragDom.style.left = `${e.clientX}px`;
                                }
                            }
                            // 往右拖拽
                            if (clientX < e.clientX) {
                                if (elW - (e.clientX - clientX) > 100) {
                                    dragDom.style.width = elW - (e.clientX - clientX) + 'px';
                                    dragDom.style.left = `${EloffsetLeft + (e.clientX - clientX)}px`;
                                }
                            }
                        }
                        // 底部鼠标拖拽位置
                        if (
                            ELscrollTop + clientY > EloffsetTop + elH - 20 &&
                            ELscrollTop + clientY < EloffsetTop + elH &&
                            dir === 'ns'
                        ) {
                            // 往上拖拽
                            if (clientY > e.clientY) {
                                if (dragDom.clientHeight < minHeight) {
                                    console.log()
                                } else {
                                    dragDom.style.height = elH - (clientY - e.clientY) + 'px';
                                    // dialogBodyEl.style.height = dialogBodyH - (e.clientY - clientY) - 20  + 'px';
                                }
                            }
                            // 往下拖拽
                            if (clientY < e.clientY) {
                                dragDom.style.height = elH + (e.clientY - clientY)  + 'px';
                                // dialogBodyEl.style.height = dialogBodyH + (e.clientY - clientY) - 20  + 'px';
                            }
                        }
                    }
                    // 拉伸结束
                    dragDom.onmouseup = function(e) {
                        e.preventDefault();
                        e.stopPropagation();
                        document.onmousemove = null
                        document.onmouseup = null
                    }
                }
            }
        },200);
    }
})

// v-dialogDragWidth: 弹窗宽度拖大 拖小
Vue.directive('dialogDragWidth', {
    bind(el, binding, vnode, oldVnode) {
        const dragDom = binding.value.$el.querySelector('.el-dialog');

        el.onmousedown = (e) => {

            // 鼠标按下，计算当前元素距离可视区的距离
            const disX = e.clientX - el.offsetLeft;

            document.onmousemove = function (e) {
                e.preventDefault(); // 移动时禁用默认事件

                // 通过事件委托，计算移动的距离
                const l = e.clientX - disX;
                dragDom.style.width = `${l}px`;
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
    }
})
// Vue.directive('drag', //自定义指令                                      JS
//     {
//         bind: function (el, binding) {
//             let oDiv = el; //当前元素
//             let self = this; //上下文
//             oDiv.onmousedown = function (e) {
//                 //鼠标按下，计算当前元素距离可视区的距离
//                 let disX = e.clientX - oDiv.offsetLeft;
//                 let disY = e.clientY - oDiv.offsetTop;

//                 document.onmousemove = function (e) {
//                     //通过事件委托，计算移动的距离
//                     let l = e.clientX - disX;
//                     let t = e.clientY - disY;
//                     if (l < 0) {
//                         l = 0;
//                     } else if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
//                         l = document.documentElement.clientWidth - oDiv.offsetWidth;
//                     }
//                     if (t < 0) {
//                         t = 0;
//                     } else if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
//                         t = document.documentElement.clientHeight - oDiv.offsetHeight;
//                     }
//                     //移动当前元素
//                     oDiv.style.left = l + 'px';
//                     oDiv.style.top = t + 'px';
//                     //将此时的位置传出去
//                     binding.value({
//                         x: e.pageX,
//                         y: e.pageY
//                     })
//                 };
//                 document.onmouseup = function (e) {

//                     document.onmousemove = null;
//                     document.onmouseup = null;
//                 };
//             };
//         }
//     }
// );
